<template>
	<view class="content">
		<view class="payment-card">
			<view class="card-block" @click="navigate('/page_other/monetization/payment')">
				<view class="bank-title">到账方式</view>
				<view class="bank-block">
					<view class="select-block" v-if="receiptInfo.type==null">请选择付款方式</view>
					<view class="select-block" v-if="receiptInfo.type==1">已选择微信/支付宝收款码</view>
					<view class="select-block" v-if="receiptInfo.type==2">
						{{receiptInfo.bankName}} {{'('+receiptInfo.bankNum+')'}}
					</view>
				</view>
				<view class="iconfont icon-gengduo more"></view>

			</view>
			<view class="payment-block">
				<view class="title FZHZGBJ">变现益联券</view>
				<view class="form-item" style="margin-bottom: 20rpx;">
					<view class="unit">￥</view>
					<view style="width: 400rpx;">
						<picker @change="bindVoucher" :value="voucherIndex" :range="voucherList" class="form-crl1">
							{{voucherIndex==-1 ? '请选择变现的益联券':voucherList[voucherIndex]}}
						</picker>
					</view>

					<view class="right">
						<view class="iconfont icon-gengduo"></view>
					</view>

				</view>
				<text class="payment-text">当前剩余益联券量{{voucher}}券</text>
			</view>

		</view>

		<view class="btn-item">
			<button class="payment-button" @click="realization">
				<text style="color: #FFFFFF;">变现</text>
			</button>
		</view>
		<loading v-if="showLoading"></loading>
		<view class="info-block">
			<text class="info-text">我的挂售</text>
			<template v-if="list.length>0">
				<view class="list-block">
					<view class="info-list" v-for="(item,index) in list" :key="item.id" @click="changeStatus(item)">
						<view v-if="item.status==1&&isShow"
							style="width: calc(50% - 40rpx); border-radius: 20rpx; display: flex; flex-direction: column; align-items: center; justify-content: center; background: rgba(0, 0, 0, 0.3); position: absolute;height: 280rpx;">
							
								<view class="delete" @click="changeSale(item.id,2)">删除</view>
								<view class="delete" style="background-color: #FF2F34;margin-top: 28rpx;"
									@click="changeSale(item.id,0)">售罄</view>
							
							
						</view>
						<view v-if="item.status==0"
							style="width: calc(50% - 40rpx); border-radius: 20rpx; display: flex; flex-direction: column; align-items: center; justify-content: center; background: rgba(0, 0, 0, 0.3); position: absolute;height: 280rpx;">
							<view class="saleTime"
								style="color: #fff;font-weight:bold;font-size: 30rpx;text-align: center;">
								{{formatDate(item.saleTime)}} 售罄
							</view>
							<image
								src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/alliance/shouxin.png"
								class="img-shouqin" mode="aspectFill"></image>
						</view>
						<image
							src="https://job-1306862033.cos.ap-chengdu.myqcloud.com/miniprogram-static/alliance/20240914111038.png"
							class="img-cover" mode="aspectFill"></image>
						<view class="describe-item">
							<view class="name">{{item.amount}}益联券</view>
							<view class="describe">{{item.businessName}}</view>
							<view class="wage">￥{{item.amount}}元</view>
						</view>
					</view>
				</view>
			</template>
			<view v-if="more==false&&list.length==0" class="empty">暂无数据</view>
			<view v-else-if="more==false&&list.length>0" class="empty">没有更多了</view>
		</view>
	</view>
</template>

<script>
	import {
		isMore
	} from '@/util/util.js'
	import {
		getStorage
	} from '@/util/auth.js';
	import {
		getCurrentUserList,
		queryMerchantVariablePresentValue,
		iWantToCashIn,
		update,
		getRemainingAmount
	} from '@/api/monetization.js'
	export default {
		data() {
			return {
				conf: {
					page: 1,
					limit: 10,
					name: '',
				},
				more: true,
				list: [],
				voucherList: [],
				voucherIndex: -1,
				tabActive: 0,
				payMoney: 0,
				changePrice: 0,
				price: '',
				isShow:false,
				form: {
					amount: 0,
					payInfoId:null,
					payType:null,
					picUrl:''
				},
				receiptInfo: {
					type: null,
					bankName: '',
					bankNum: ''
				},
				voucher:0
			}
		},
		methods: {
			realization()
			{
				if(this.form.amount==0||this.form.amount==''||this.form.payType==null)
				{
					this.showToast('请检查付款方式及变现益联券')
					return;
				}
			
				iWantToCashIn(this.form).then(res=>{
					if(res=='success')
					{
						this.showToast('发布成功')
						uni.redirectTo({
							url:'/page_other/selling_coupons/index'
						})
					}else{
						this.showToast(res)
					}
				})
			},
			bindVoucher(e) {
				this.voucherIndex = e.detail.value
				this.form.amount = this.voucherList[this.voucherIndex]
			},
			getLastName(name, length) {
				if (name.length > length) {
					return name.substring(name.length - length);
				} else {
					return name;
				}
			},
			getRemainingAmount()
			{
				getRemainingAmount().then(res=>{
					this.voucher=res
				})
			},
			changeStatus(item) {
				this.isShow = true;
			},
			changeSale(id,status) {
				
				update({
					id:id,
					status:status
				}).then(res=>{
					console.log(res)
					this.showToast('操作成功')
					this.getList(true);
				})
			},
			changeActive(e, index) {
				this.tabActive = e
				this.changePrice = this.list[index].quota
				this.price = this.changePrice
			},
			queryMerchantVariablePresentValue() {
				queryMerchantVariablePresentValue({}).then(res => {
					this.voucherList = res

				})
			},
			changeInput() {
				this.tabActive = 0;
				this.changePrice = 0;
			},
			search() {
				this.getList(true)
			},
			formatDate(saleTime) {
			        const dateObj = new Date(saleTime);
			        const month = String(dateObj.getMonth() + 1).padStart(2, '0'); // Month is zero-indexed
			        const day = String(dateObj.getDate()).padStart(2, '0');
			        const hours = String(dateObj.getHours()).padStart(2, '0');
			        const minutes = String(dateObj.getMinutes()).padStart(2, '0');
			        const seconds = String(dateObj.getSeconds()).padStart(2, '0');
			        const formattedDate = `${month}-${day} ${hours}:${minutes}`;
			    
			        return formattedDate;
			},
			handleRefresh() {
				this.conf.page = 1;
				this.more = true
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				this.getList(true)
			},
			getList(refresh = false) {
				return getCurrentUserList(this.conf).then((res) => {
					let list = res.list || [];
					list.forEach(item => {
						this.isShow = false;
						item.isSaleShow = false;
					})
					if (refresh) {
						this.list = list;
					} else {
						this.list = [...this.list, ...list]
					}
					this.more = isMore(this.conf.page, this.conf.limit, res.count)
				})
			}
		},
		onPullDownRefresh() {
			this.handleRefresh();
		},
		onReachBottom() {
			if (this.more) {
				this.conf.page++;
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				this.getList();
			}
		},
		async onLoad(e) {

			uni.showCustomLoading();
			Promise.all([this.getList(true), this.queryMerchantVariablePresentValue(),this.getRemainingAmount()]).finally(() => {
				uni.hideCustomLoading()
			})
			if (e.info) {
				this.receiptInfo = JSON.parse(e.info)
				if (this.receiptInfo.type == 2) {
					// 使用 slice 方法截取最后四位数字
					let lastFourDigits = this.receiptInfo.bankNum.slice(-4);
					this.receiptInfo.bankNum = lastFourDigits
					
				}
				this.form.payType=this.receiptInfo.type
				this.form.payInfoId=this.receiptInfo.id
				this.form.picUrl=this.receiptInfo.url
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		//height: calc(100vh - 160rpx);
		overflow-y: auto;
		position: relative;

		.payment-text {
			margin-top: 30rpx;
			font-size: 28rpx;
			width: 354px;
			height: 18px;
			opacity: 1;
			font-family: Microsoft YaHei;
			font-weight: normal;
			line-height: normal;
			letter-spacing: 0em;
			/* 正文色/正文辅助色 */
			color: #767676;
		}

		.search-block {
			width: 100%;
			height: 200rpx;
			// margin-top: 180rpx;
			background-image: linear-gradient(to right, #FF3432, #FF671A);
			padding: 30rpx;
		}

		.search-item {
			width: 100%;
			height: 70rpx;
			display: flex;


			.address-block {
				display: flex;
				background-color: #FFFFFF;
				align-items: center;
				justify-content: center;
				width: 130rpx;
				border-radius: 50px;
				margin-right: 20rpx;

				image {
					width: 30rpx;
					height: 30rpx;
					margin-right: 5rpx;
				}

				.address {
					color: #FF2F34;
					font-size: 28rpx;
				}
			}

			.input-block {
				display: flex;
				background-color: #FFFFFF;
				border-radius: 50px;
				align-items: center;
				justify-content: space-between;
				width: 100%;
				padding: 6rpx 6rpx 6rpx 20rpx;

				.input-left {
					width: calc(100% - 140rpx);
					display: flex;

					image {
						width: 30rpx;
						height: 30rpx;
						// margin-right: 5rpx;
						margin: 7rpx 7rpx 0 0;
					}

					input {
						border-radius: 50px;
						width: 100%;

					}
				}

				.search {
					display: flex;
					justify-content: center;
					align-items: center;
					width: 130rpx;
					border-radius: 50rpx;
					height: 100%;
					padding-bottom: 5rpx;
					color: #FFFFFF;
					background: linear-gradient(90deg, #FF2F34 0%, #FF6B18 100%);
				}
			}
		}


		.info-block {
			display: flex;
			flex-direction: column;
			background: #F5F9FC;
			border-radius: 40rpx;
			margin-top: 2rpx;
			height: auto;
			position: relative;
			padding: 30rpx;
			min-height: 70vh;

			.info-text {
				font-family: Microsoft YaHei;
				font-size: 16px;
				font-weight: bold;
				line-height: 22px;
				letter-spacing: 0px;
				/* 正文色/正文色 */
				color: #1A1A1A;
			}

			.list-block {
				display: flex;
				flex-direction: row;
				flex-wrap: wrap;
				margin-bottom: 30rpx;


				.info-list {
					background: #fff;
					margin-top: 20rpx;
					border-radius: 20rpx;
					width: calc((100% - 20rpx) / 2);
					margin-right: 20rpx;
					height: 280rpx;

					.img-shouqin {
						margin-top: 10rpx;
						width: 80%;
						height: 140rpx;
						border-radius: 20rpx 20rpx 0 0;
					}
					.img-cover{
						width: 100%;
						height: 150rpx;
						border-radius: 20rpx 20rpx 0 0;
					}

					.delete {
						width: 100rpx;
						height: 100rpx;
						border-radius: 50%;
						background-color: #000;
						color: #fff;
						display: flex;
						align-items: center;
						justify-content: center;
					}

					.img {
						width: 100%;
						height: 360rpx;
						border-radius: 20rpx 20rpx 0 0;
					}

					.describe-item {
						padding: 0 30rpx;

						.name {
							font-size: 28rpx;
							font-weight: bold;
							margin-bottom: 5rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
						}

						.describe {
							color: #828282;
							font-size: 28rpx;
							margin-bottom: 5rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							white-space: nowrap;
						}

						.wage {
							color: #FF2F34;
							font-weight: bold;
							font-size: 28rpx;

							.txt {
								font-size: 38rpx;
								margin-right: 10rpx;
							}
						}
					}
				}

				.info-list:nth-child(2n) {
					margin-right: 0;
				}
			}

			.empty {
				display: flex;
				justify-content: center;
			}
		}

		.payment-card {

			width: calc(100% - 60rpx);
			margin: 20rpx auto;
			border-radius: 20rpx;
			height: 460rpx;
			background: linear-gradient(90deg, #FF2F34 0%, #FF6B18 100%);

			.card-block {
				height: 200rpx;
				display: flex;
				justify-content: space-between;
				width: 100%;

				.bank-title {
					padding-left: 20rpx;
					height: 200rpx;
					line-height: 200rpx;
					font-size: 32rpx;
					color: #fff;
					width: 200rpx;

				}

				.bank-block {

					width: 500rpx;
					padding-left: 20rpx;
					height: 200rpx;
					line-height: 200rpx;
					font-size: 32rpx;
					color: #fff;

				}

				.more {
					width: 100rpx;
					line-height: 200rpx;
					font-size: 32rpx;
					color: #fff;
				}
			}

			.payment-block {
				// width: calc(100% - 60rpx);
				height: 260rpx;
				background-color: #FFFFFF;
				padding: 30rpx;
				border-radius: 20rpx;

				.payment-info {
					display: flex;
					align-items: center;
					flex-wrap: wrap;
					margin-top: 30rpx;

					.payment-list:nth-child(3n) {
						margin-right: 0;
					}

					.payment-list.active {
						background-color: #4071E5;
						color: #FFFFFF;
					}

					.payment-list {
						width: calc((100% - 40rpx) / 3);
						margin-right: 20rpx;
						margin-bottom: 20rpx;
						background: #F6F6F6;
						padding: 24rpx 30rpx;
						display: flex;
						align-items: center;
						justify-content: center;
						font-weight: bold;
						border-radius: 15rpx;
						font-size: 30rpx;
					}
				}

				.form-item {
					display: flex;
					align-items: center;
					justify-content: space-around;
					border-bottom: 1px solid #F7F7F7;
					line-height: 90rpx;
					font-size: 30rpx;

					.unit {
						font-weight: bold;
						width: 20rpx;
					}

					.form-crl {
						width: calc(100% - 70rpx);
					}

					.iconfont {
						color: #767676;
						font-size: 34rpx;
					}
				}

				.btn-item {
					display: flex;
					justify-content: center;
					margin-top: 40rpx;
				}
			}

		}

		.payment-button {
			width: 288px;
			height: 50px;
			border-radius: 50px;
			opacity: 1;
			background: linear-gradient(90deg, #FF2F34 0%, #FF6B18 100%);
			box-shadow: 0px 4px 10px 0px rgba(0, 0, 0, 0.1);
		}

		.top-block {
			padding: 180rpx 30rpx 20rpx 30rpx;
			background-image: linear-gradient(to bottom, #3153B6, #4173E7);

			.info-block {
				background: #FFFFFF;
				padding: 30rpx;
				border-radius: 20rpx;

				.title-item {
					display: flex;
					align-items: center;
					justify-content: space-between;
				}

				.title {
					margin-bottom: 30rpx;
					font-size: 32rpx;
				}

				.price-item {
					display: flex;
					align-items: center;
					color: #4376E4;
					font-size: 28rpx;
					margin-bottom: 20rpx;

					.price {
						font-size: 44rpx;
						margin-right: 10rpx;
					}
				}

				.info-item {
					display: flex;
					font-size: 30rpx;
					line-height: 65rpx;

					.name {
						width: 160rpx;
						color: #767676;
					}

					.text {
						width: calc(100% - 160rpx);
					}
				}
			}
		}
	}
</style>